
package info.niwota.dc;

import info.niwota.dc.HourMinutePicker.OnTimeChangedListener;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

public class TimePickerDialog extends AlertDialog implements OnClickListener, 
        OnTimeChangedListener {

    public interface OnTimeSetListener {
        void onTimeSet(HourMinutePicker view, int hourOfDay, int minute);
    }

    private static final String HOUR = "hour";
    private static final String MINUTE = "minute";
    
    private final HourMinutePicker mTimePicker;
    private final OnTimeSetListener mCallback;
    
    private final Calendar mCalendar;
    
    private final java.text.DateFormat mDateFormat = new SimpleDateFormat("HH:mm MMM dd, yyyy");
    
    int mInitialHourOfDay;
    int mInitialMinute;
    
	private long mDate;
    
    public TimePickerDialog(Context context,
            OnTimeSetListener callBack,
            int hourOfDay, int minute, long date) {
    	this(context, android.R.style.Theme_Dialog,
                  callBack, hourOfDay, minute, date);
    }

    public TimePickerDialog(Context context,
            int theme,
            OnTimeSetListener callBack,
            int hourOfDay, int minute, long date) {
        super(context);
        
        mCallback = callBack;
        mInitialHourOfDay = hourOfDay;
        mInitialMinute = minute;
        
       // mDateFormat = DateFormat.getTimeFormat(context);
        mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(date);
        mDate = date;
        
        updateTitle(mInitialHourOfDay, mInitialMinute);
        
        setButton(context.getText(R.string.action_ok), this);
        setButton2(context.getText(R.string.action_cancel), (OnClickListener) null);
        
        setIcon(R.drawable.ic_dialog_time);
        
        LayoutInflater inflater = 
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.time_picker_dialog, null);
        setView(view);
        mTimePicker = (HourMinutePicker) view.findViewById(R.id.timePicker);

        // initialize
        mTimePicker.setCurrentHour(mInitialHourOfDay);
        mTimePicker.setCurrentMinute(mInitialMinute);
       // mTimePicker.setIs24HourView(true);
        mTimePicker.setOnTimeChangedListener(this);
    }
    
    public void onClick(DialogInterface dialog, int which) {
        if (mCallback != null) {
            mTimePicker.clearFocus();
            mCallback.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(), 
                    mTimePicker.getCurrentMinute());
        }
    }

    public void onTimeChanged(HourMinutePicker view, int hourOfDay, int minute) {
        updateTitle(hourOfDay, minute);
    }
    
    public void updateTime(int hour, int minute) {
        mTimePicker.setCurrentHour(hour);
        mTimePicker.setCurrentMinute(minute);
        //
        updateTitle(hour, minute);
    }

    private void updateTitle(int hour, int minute) {
    	mCalendar.setTimeInMillis(mDate);
    	mCalendar.add(Calendar.HOUR_OF_DAY, -hour);
    	mCalendar.add(Calendar.MINUTE, -minute);
    	
        //mCalendar.set(Calendar.HOUR_OF_DAY, hour);
        //mCalendar.set(Calendar.MINUTE, minute);
       
        setTitle(mDateFormat.format(mCalendar.getTime()));
    }
    
    @Override
    public Bundle onSaveInstanceState() {
        Bundle state = super.onSaveInstanceState();
        state.putInt(HOUR, mTimePicker.getCurrentHour());
        state.putInt(MINUTE, mTimePicker.getCurrentMinute());
        
        return state;
    }
    
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        
        int hour = savedInstanceState.getInt(HOUR);
        int minute = savedInstanceState.getInt(MINUTE);
        
        //mTimePicker.setIs24HourView(true);
        mTimePicker.setCurrentHour(hour);
        mTimePicker.setCurrentMinute(minute);
        
        mTimePicker.setOnTimeChangedListener(this);
        //
        updateTitle(hour, minute);
    }
}
